using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Net.Mail;

public partial class ForgotUsername : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            SetActiveButtonAttributes(SendEmailButton);
            SetActiveButtonAttributes(CancelButton);
            if (Request.IsAuthenticated)
                Response.Redirect("/default.aspx");
        }
    }

    private void SetActiveButtonAttributes(Button button)
    {
        button.Attributes.CssStyle.Add("background-color", "#eee");
        button.Attributes.Add("OnMouseOut", "this.style.backgroundColor ='#eee';");
        button.Attributes.CssStyle.Add("text-decoration", "none");
        button.Attributes.CssStyle.Add("margin", "3px");
        button.Attributes.CssStyle.Add("padding", "7px 6px 7px 6px");
        button.Attributes.Add("OnMouseOver", "this.style.backgroundColor ='#ddd';");
    }

    protected void Cancel_Click(object sender, EventArgs e)
    {
        Response.Redirect("/default.aspx");
    }

    protected void SendEmail_Click(object sender, EventArgs e)
    {
        if (txtEmail.Text.Length == 0)
        {
            ResultsText.Text = "Please enter your email address";
            return;
        }
        string strName = Membership.GetUserNameByEmail(txtEmail.Text);
        if (String.IsNullOrEmpty(strName))
        {
            ResultsText.Text = "We're sorry.  That email address is not in our database. Please try again";
            return;
        }
        
        MembershipUser user = Membership.GetUser(strName);
        if (user == null)
        {
            ResultsText.Text = "We're sorry.  That email address is not in our database. Please try again";
            return;
        }

        MailMessage msg = new MailMessage();

        AddFromAndSubject(msg, user);
        AddBody(msg, user);
        SendIt(msg);

        ResultsText.Text = "Your account information has been sent to " + txtEmail.Text + " The password was reset to enable you to log into Sueetie.org temporarily.  You can change your password after logging in with the new password.";
    }
 
       private MailMessage AddFromAndSubject(MailMessage _msg, MembershipUser _user)
        {
            _msg.From = new MailAddress("admin@sueetie.org", "Sueetie User Services");
            _msg.Subject = "Your Sueetie.org Account Information";

            ProfileCommon profile = Profile.GetProfile(_user.UserName);
            MailAddress userAddress = new MailAddress(_user.Email, profile.Personal.DisplayName);
           _msg.To.Add(userAddress);
            return _msg;
        }

            private MailMessage AddBody(MailMessage _msg, MembershipUser _user)
        {

            string body = string.Empty;
            _msg.IsBodyHtml = true;

            body += "<div style=\"font-family: Arial; font-size: 16px; margin-bottom: 15px;\">";
            body += "As requested, your Sueetie Account Information is below:</div>";
            body += "<div style=\"font-family: Arial; font-size: 14px; margin-bottom: 25px;\">";
            body += "</div>";
            body += "<div style=\"font-family: Arial; font-size: 14px; font-weight: bold; margin-bottom: 15px;\">";
            body += "Your Account Username: " + _user.UserName + "<br />";
            body += "Temporary Password: " + _user.ResetPassword() + "<br />";
            body += "</div>";
        
            body += "<div style=\"font-family: Tahoma; font-size: 11px; font-weight: normal; margin-bottom: 15px;\">";
            body += "_______________________________________________________<br /><br />";
            body += "<a href=\"http://sueetie.org\">Sueetie Home Page</a></div>";

            _msg.Body = body;
            return _msg;
        }

    private void SendIt(MailMessage _msg)
        {
            SmtpClient client = new SmtpClient();
            client.Send(_msg);
        }

}
